$(function() {
	$('#vue_vm_list div').hide();
	$('#houseTypeType_vipCard').hide();
	$('#houseTypeType_ordinary').hide();
});

var beHospitalFees = {
	beHospitalFeesId: '',
	elderId: '',
	isFinancialCs: '',
	bankName: '',
	contactName: '',
	contactPhoneNumer: '',
	beHospitalTime: '',
	outHospitalTime: '',
	subsidyApply: '',
	pledgePay: '',
	mealCardPay: '',
	elderWalletPay: '',
	electricityPay: '',
	waterPay: '',
	hotWaterPay: '',
	totalPay: '',
	realityPay: '',
	createTime: '',
	updateTime: '',
	createId: getLoginedUser()? getLoginedUser().staffId: '',
	createName: getLoginedUser()? getLoginedUser().staffName: '',
	updateId: getLoginedUser()? getLoginedUser().staffId: '',
	updateName: getLoginedUser()? getLoginedUser().staffName: '',
	bedCost: '',
	nursingCost: '',
	baseServiceCost: ''
};

var view = {
	data: beHospitalFees
};

var vue_vm = new Vue({
	el: '#vue_vm',
	data: view
});

function save() {
	feesInputChange(); //为了防止事件的响应不及时，在提交之前必须运行一次事件处理代码。
	
	if(view.data.elderId=='') {
		alert('请先查询长者');
		return;
	}
	
	if($('#isFinancialCs').is(':checked')) {
		view.data.isFinancialCs = 1;
		view.data.bankName = $('#bankName').val();
		view.data.contactName = $('#contactName').val();
		view.data.contactPhoneNumer = $('#contactPhoneNumer').val();
	}else {
		view.data.isFinancialCs = 2;
		view.data.bankName = null;
		view.data.contactName = null;
		view.data.contactPhoneNumer = null;
	}
	
	view.data.beHospitalTime = $('#beHospitalYear').val() + '-' + $('#beHospitalMonth').val() + '-' + $('#beHospitalDay').val();
	view.data.outHospitalTime = $('#outHospitalYear').val() + '-' + $('#outHospitalMonth').val() + '-' + $('#outHospitalDay').val();
	var option = $('#foodAllowance_select option:selected');
	view.data.allowanceName = option.text();
	view.data.subsidyApply = view.data.allowanceName;
	
	view.data.pledgePay = (view.data.pledgePay==''? 0: parseFloat(view.data.pledgePay));
	view.data.mealCardPay = (view.data.mealCardPay==''? 0: parseFloat(view.data.mealCardPay));
	view.data.elderWalletPay = (view.data.elderWalletPay==''? 0: parseFloat(view.data.elderWalletPay));
	view.data.electricityPay = (view.data.electricityPay==''? 0: parseFloat(view.data.electricityPay));
	view.data.waterPay = (view.data.waterPay==''? 0: parseFloat(view.data.waterPay));
	view.data.hotWaterPay = (view.data.hotWaterPay==''? 0: parseFloat(view.data.hotWaterPay));
	view.data.totalPay = (view.data.totalPay==''? 0: parseFloat(view.data.totalPay));
	view.data.realityPay = (view.data.realityPay==''? 0: parseFloat(view.data.realityPay));
	view.data.bedCost = view.data.bedCostForHalfMoon;
	view.data.nursingCost = view.data.nursingCostForHalfMoon;
	view.data.baseServiceCost = view.data.baseServiceCostForHalfMoon;

	save_basic(path+'/beHospitalFees/save', {beHospitalFees:view.data}, function() {
		alert('入院缴费成功');
		window.open('3-3-1ryjiaofei-1.html?pageParam='+encodeURIComponent(JSON.stringify(view.data)));
	});
}

function findElder() {
	if($('#vue_vm_list').length==0) {return;}
	$('#vue_vm_list div').hide();
	var searchVal = $('#searchVal').val();
	
	find_basic(path+'/beHospitalFees/findElderInfo', searchVal, function(response) {	
		$('#vue_vm_list').html(gl_vue_vm_list_html);
		
		if(response.data.length>0) {
			
		}
		
		new Vue({
			el: '#vue_vm_list',
			data: {
				todos: response.data
			},
			methods: {
				getSubsidizationValue:function(subsidization, subsidizationValue) {
					return subsidization=='无补助'? '': subsidizationValue;
				},
				getSubsidizationUnit:function(subsidization) {
					return subsidization=='饭卡补助'? '元': (subsidization=='包餐补助'?'天':'');
				},
				getSubsidizationText:function(subsidization, subsidizationValue) {
					return subsidization + this.getSubsidizationValue(subsidization, subsidizationValue) + this.getSubsidizationUnit(subsidization);
				},
				getConsDeductionText:function(consumptionWay, consDeductionWay) {
					if(!consumptionWay||consumptionWay=='') {
						consumptionWay = '半个月扣费';
					}
					return consumptionWay+consDeductionWay+'元';
				},
				//总共可扣月数
				getTotalMonths:function(todo) {
					return 12;
				},
				//剩余可扣月数
				getRemainMonths:function(todo) {
					return todo.surplusMonths;
				},
				//已扣月数
				getAlreadyUsedMonths:function(todo) {
					var alreadyUsedMonths = this.getTotalMonths(todo) - this.getRemainMonths(todo);
					return alreadyUsedMonths;
				},
				//总共可扣次数
				getTotalNumber:function(todo) {
					if(todo.consumptionWay=='半个月扣费') {
						return 24;
					}else {
						return 12;
					}
				},
				//剩余可扣次数
				getRemainNumber:function(todo) {
					return todo.buckleNumber;
				},
				//已扣次数
				getAlreadyUsedNumber:function(todo) {
					var alreadyUsedNumber = this.getTotalNumber(todo) - this.getRemainNumber(todo);
					return alreadyUsedNumber;
				}
			}
		});
		
		view.data.elderId = response.data[0].elder.elderId;
		view.data.elderName = response.data[0].elder.name;
		view.data.sex = response.data[0].elder.sex;
		view.data.birthday = response.data[0].elder.birthday;
		view.data.peoples = response.data[0].elder.peoples;
		view.data.cardId = response.data[0].elder.cardId;
		view.data.maritalStatus = response.data[0].elder.maritalStatus;
		view.data.registeredResidence = response.data[0].elder.registeredResidence;
		view.data.liveAddress = response.data[0].elder.liveAddress;
		view.data.subjectRoomName = response.data[0].subjectRoom.roomName;
		view.data.buildingName = response.data[0].building.buildingName;
		view.data.floorName = response.data[0].floor.floorName;
		view.data.roomName = response.data[0].room.roomName;
		view.data.roomType = response.data[0].room.roomType;
		view.data.bedNumber = response.data[0].bed.bedNumber;
		view.data.nursingDesc = response.data[0].nursingCost.nursingCostName;
		view.data.baseServiceDesc = response.data[0].baseServiceCost.serviceName;
		view.data.userVipCardForm = response.data[0].userVipCardForm;
		view.data.elderBeHospitalForm = response.data[0].elderBeHospitalForm;
		
		view.data.nursingCostForHalfMoon = (response.data[0].nursingCost.price/2).toFixed(2);
		$('#nursingCostForHalfMoon').text(view.data.nursingCostForHalfMoon+'元（半个月）');
		
		/*
		view.data.baseServiceCostForHalfMoon = (response.data[0].baseServiceCost.price/2).toFixed(2);
		$('#baseServiceCostForHalfMoon').text(view.data.baseServiceCostForHalfMoon+'元（半个月）');
		*/
		
		if(response.data[0].baseServiceCost.deductionWay=='半个月扣费') {
			view.data.baseServiceCostForHalfMoon = (response.data[0].baseServiceCost.price/2).toFixed(2);
			$('#baseServiceCostForHalfMoon').text(view.data.baseServiceCostForHalfMoon+'元（半个月）');
		}else {
			view.data.baseServiceCostForHalfMoon = response.data[0].baseServiceCost.price;
			$('#baseServiceCostForHalfMoon').text(view.data.baseServiceCostForHalfMoon+'元（一个月）');
		}
		
		view.data.houseTypeType = response.data[0].elderBeHospitalForm.houseTypeType;
		
		if(view.data.houseTypeType==1) {
			$('#houseTypeType').text('来源：VIP卡');
			if(response.data[0].userVipCardForm.consumptionWay=='半个月扣费') {
				view.data.bedCostForHalfMoon = response.data[0].userVipCardForm.pricePerDay*15;
				$('#bedCostForHalfMoon').text(view.data.bedCostForHalfMoon+'元（半个月）');
			}else {
				view.data.bedCostForHalfMoon = response.data[0].userVipCardForm.pricePerDay*30;
				$('#bedCostForHalfMoon').text(view.data.bedCostForHalfMoon+'元（一个月）');
			}
		}else {
			view.data.bedCostForHalfMoon = (response.data[0].elderBeHospitalForm.bedCostPrice/2).toFixed(2);
			$('#houseTypeType').text('来源：现金');
			$('#bedCostForHalfMoon').text(view.data.bedCostForHalfMoon+'元（半个月）');
		}
		
		$('#vue_vm_list div').show();
		
		load_foodAllowance_select();
	});
}

function addBeTime() {
	var elder_add_month = {};
	elder_add_month.elderId = $('#elderId').val();
	elder_add_month.monthNum = parseInt($('#monthNum').val());

	save_basic(path+'/beHospitalFees/addBeTime', elder_add_month, function() {
		alert('住院时长增加成功');
	});
}

function beHospitalFeesFind() {
	if($('#vue_vm_list').length==0) {return;}
	$('#vue_vm_list div').hide();
	var searchVal = $('#searchVal').val();

	find_basic_by_json(path+'/beHospitalFees/find', {searchVal: searchVal}, function(response) {
		$('#vue_vm_list').html(gl_vue_vm_list_html);
		
		for(var i=0; i<response.data.length; i++) {
			var outHospitalTime = new Date(response.data[i].outHospitalTime).getTime();
			var beHospitalTime = new Date(response.data[i].beHospitalTime).getTime();
			var remainDayNum = Math.floor((outHospitalTime-beHospitalTime)/(24*3600*1000));
			response.data[i].remainDayNum = remainDayNum? remainDayNum: 0;
		}
		
		new Vue({
			el: '#vue_vm_list',
			data: {
				todos: response.data
			}
		});
		
		$('#vue_vm_list div').show();
	});
}

function deleteBatch() {
	deleteBatch_basic(path+'/beHospitalFees/deleteBatch');
}

function feesInputChange() {
	var variablePay = parseFloat($('#pledgePay').val()) + parseFloat($('#mealCardPay').val()) + parseFloat($('#elderWalletPay').val())
				+ parseFloat($('#electricityPay').val()) + parseFloat($('#waterPay').val()) + parseFloat($('#hotWaterPay').val());
				
	view.data.totalPay = variablePay + parseFloat(view.data.nursingCostForHalfMoon)
				+ parseFloat(view.data.baseServiceCostForHalfMoon)
				+ parseFloat(view.data.bedCostForHalfMoon);
				
	view.data.realityPay = view.data.totalPay;
	
	$('#realityPay').text(view.data.realityPay+'元');
	$('#totalPay').text(view.data.totalPay+'元');
}